package com.webdrider.model.validators;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractSessionRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.errors.SessionError;
import com.webdrider.util.SessionCacheContainer;

public class SessionValidator extends AbstractValidator {

	@Override
	public boolean validate(AbstractRequest abstractRequest) {
		AbstractSessionRequest request = null;
		
		if (abstractRequest instanceof AbstractSessionRequest) {
			request = (AbstractSessionRequest) abstractRequest;
		} else {
			error = new SessionError(AbstractError.INVALID_REQUEST);
			return false;
		}
		
		String sessionId = request.getSessionId();
		
		if (sessionId == null || sessionId.length() == 0) {
			error = new SessionError(SessionError.NO_SESSION_PROVIDED);
			return false;
		}
		
		if (!SessionCacheContainer.containsAndRefresh(sessionId)) {
			error = new SessionError(SessionError.SESSION_NOT_FOUND);
			return false;
		}
		
		return true;
	}
}
